import axios from 'axios'
import router from '@/router'
import { enterLogin } from '@/tool'
const fetch = axios.create({
  baseURL: process.env.VUE_APP_BASEURL,
  headers: {
    "version": "1.0.0",
    "device": "win"
  }
})


fetch.interceptors.request.use((req) => {
  const token = window.localStorage.getItem("Token") || ''
  Reflect.set(req.headers, 'Authorization', token)
  return req
})

fetch.interceptors.response.use(res => {
  const { data = {} } = res
  if (data.code === 401) {
    router.replace('/login')
    enterLogin()
  }
  if (data.code === 500) {
    return Promise.reject(data)
  }
  if (data?.code === 200) {
    return data?.data || data
  }
  return data
})

export default fetch